package com.linkgie.galaxyframework.utils;

public class ReadonlyArrayListPage<T> extends ReadonlyArrayList<T> implements ListPage<T> {

	private final InnerPagination pagination;

	public ReadonlyArrayListPage(T[] items, int offset, int size) {
		this(items, offset, size, 0, size, (long) items.length);
	}

	public ReadonlyArrayListPage(T[] items, int offset, int size, long pageIndex, long pageSize, Long totalSize) {
		super(items, offset, size);

		this.pagination = new InnerPagination(pageIndex, pageSize, totalSize);
	}

	public ReadonlyArrayListPage(T[] items, int offset, int size, Pagination pagination) {
		super(items, offset, size);

		this.pagination = new InnerPagination(pagination);
	}

	/**
	 * 设置分页信息；
	 * 
	 * @param pageIndex 当前页索引；
	 * @param pageSize  页大小；
	 * @param totalSize 总记录数；
	 *                  <p>
	 *                  如果设置为负数，表示不知道总记录数，最终总记录数被设置为
	 *                  {@link Pagination#UNKNOWN_SIZE}；
	 */
	public void setPagination(long pageIndex, long pageSize, Long totalSize) {
		this.pagination.setPagination(pageIndex, pageSize, totalSize);
	}

	@Override
	public long getTotalSize() {
		return pagination.getTotalSize();
	}

	@Override
	public long getPageIndex() {
		return pagination.getPageIndex();
	}

	@Override
	public long getPageSize() {
		return pagination.getPageSize();
	}

	private class InnerPagination extends ListPagination {

		public InnerPagination(Pagination pagination) {
			super(pagination);
		}

		public InnerPagination(long pageIndex, long pageSize, Long totalSize) {
			super(pageIndex, pageSize, totalSize);
		}

		@Override
		int listSize() {
			return ReadonlyArrayListPage.this.size();
		}

	}

}
